package com.umeox.watch.moto.launcher.ui;


import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.watch.themes.ItemClickListener;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.app.ThemeManager;
import com.umeox.moto.watch.themes.dialog.ActionOptionsDialog;
import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.adapter.ThemeAdapter;
import com.umeox.watch.moto.launcher.bean.ThemeInfo;
import com.umeox.watch.moto.launcher.ui.help.GalleryLayoutManager;
import com.umeox.watch.moto.launcher.ui.help.ScaleTransformer;

import java.util.ArrayList;
import java.util.List;


public class ThemeSettingActivity extends ThemeAppCompatActivity implements ItemClickListener {


    public static final int[] DESCS = {R.string.apply_theme_str};
    public static final int[] ICONS = {R.drawable.icon_apply_theme};
    public static final String ACTION_THEME_LOADING_COMPLETED = "ThemeLoadingCompleted";
    RecyclerView mWatchFaceRv;
    private List<ThemeInfo> mDataList;
    private TextView mCurrentThemeTv;
    private int mIndex;
    private ThemeAdapter mAdapter;
    private String[] mThemeNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_theme_setting);
        mThemeNames = getResources().getStringArray(R.array.theme_name_phone);
        mWatchFaceRv = findViewById(R.id.rv);
        mCurrentThemeTv = findViewById(R.id.current_theme_tv);
        setThemeStatus();

        final String[] themeIcons = getResources().getStringArray(R.array.theme_icon);
        final int[] themeColors = getResources().getIntArray(R.array.theme_color);
        final int[] themeNameColors = getResources().getIntArray(R.array.theme_name_color);
        mDataList = new ArrayList<>();
        for (int i = 0; i < themeIcons.length; i++) {
            final String themeIcon = themeIcons[i];
            final int themeIconId = getResources().getIdentifier(themeIcon, "drawable", getPackageName());
            mDataList.add(new ThemeInfo(themeIconId, mThemeNames[i], themeNameColors[i], themeColors[i]));
        }


        mDataList.get(mIndex).setCurrentTheme(true);
        GalleryLayoutManager layoutManager = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
        layoutManager.setItemTransformer(new ScaleTransformer());
        layoutManager.attach(mWatchFaceRv, mIndex);
        mWatchFaceRv.setHasFixedSize(true);
        mAdapter = new ThemeAdapter(mDataList, this, R.layout.item_watch_face);
        mWatchFaceRv.setAdapter(mAdapter);


        final Intent intent = new Intent();
        intent.setAction(ACTION_THEME_LOADING_COMPLETED);
        sendBroadcast(intent);

    }

    private void setThemeStatus() {
        mIndex = ThemeManager.getInstance().getCurrentTheme();
        mCurrentThemeTv.setText(mThemeNames[mIndex]);
    }


    @Override
    public void onItemClicked(int position) {

        ActionOptionsDialog optionsDialog = new ActionOptionsDialog(this, DESCS, ICONS, this);

        optionsDialog.setOnClickListener(new ActionOptionsDialog.OnClickListener() {
            @Override
            public void onOption1Click() {
                if (position != mIndex) {
                    ThemeLoadingActivity.onLaunch(ThemeSettingActivity.this, position);
                }

            }

            @Override
            public void onOption2Click() {

            }

            @Override
            public void onOption3Click() {

            }

            @Override
            public void onOption4Click() {

            }
        });
        optionsDialog.show();

    }

}
